// addressPackage/pages/addressadd/addressadd.js
import { areaList } from '@vant/area-data';
import {ajaxFun} from "../../../utils/ajax"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    areaList,
    areablo:true,

    phone:"",//手机号
    username:"",//用户名
    chooseAddress:"",//地址
    checked:false,//是否打开默认地址开关
    detailAddress:"",//地址详情
    descObj:{}
  },
  onLoad(options){
    // console.log(options.id);
    if(options.id){
        console.log("修改和数据回显");
        wx.setNavigationBarTitle({
          title: '修改地址',
        })
        //ajax请求
        this.getAddressDesc(options.id)
    }else{
      console.log("添加");
      wx.setNavigationBarTitle({
        title: '添加地址',
      })
    }

  },
async getAddressDesc(addressId){
   let result=await ajaxFun(`/address/${addressId}`,"get");
   this.setData({
     descObj:result.data,
     phone:result.data.userPhone,//手机号
     username:result.data.userName,//用户名
     chooseAddress:`${result.data.provinceName},${result.data.cityName},${result.data.regionName}`,//地址
     checked:result.data.defaultFlag==1?true:false,//是否打开默认地址开关
     detailAddress:result.data.detailAddress,//地址详情
   })
 },
  onChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  },
  showArea(){
      this.setData({
        areablo:false
      })
  },
  confirmFun(e){//地区选择确定
    console.log(e.detail.values);
     let newArry=[];
     e.detail.values.map(item=>{
         newArry.push(item.name)
     })
     this.setData({
      chooseAddress:newArry
     },()=>{
      this.setData({
        areablo:true
      })
     })
  },
  cancelFun(){
    this.setData({
      areablo:true
    })
  },
submitBtn(){//提交
    if(this.data.username==""){
      wx.showToast({
        title: '用户名不能为空',
        icon:"none"
      })
      return false
    }
    let  reg=/^1[3|4|5|6|7|8|9]\d{9}$/;
    if(!reg.test(this.data.phone)){
      wx.showToast({
        title: '手机号不正确',
        icon:"none"
      })
      return false
    }
    if(this.data.chooseAddress==""){
      wx.showToast({
        title: '地址不能为空',
        icon:"none"
      })
      return false
    }
    if(this.data.detailAddress==""){
      wx.showToast({
        title: '输入具体地址',
        icon:"none"
      })
      return false
    }
    if(this.data.descObj.addressId){//修改
        console.log("修改提交");
        this.editorFun()
    }else{
      this.addFun()
    }
  },

 async addFun(){
    let addressArry=this.data.chooseAddress.split(",");
    console.log(addressArry);
    let  result=await ajaxFun("/address","post",{
      "userName": this.data.username,
      "userPhone": this.data.phone,

      "provinceName": addressArry[0],
      "cityName": addressArry[1],
      "regionName": addressArry[2],

      "defaultFlag": this.data.checked?1:0,// 1默认 0不选中
      "detailAddress": this.data.detailAddress
     
     
   
    })
    if(result.resultCode==200){
      wx.navigateTo({
        url: '/addressPackage/pages/addresslist/addresslist',
      })
    }
  },
  async editorFun(){
    let addressArry=this.data.chooseAddress.split(",");
     let result=await ajaxFun("/address","put",{
      "userName": this.data.username,
      "userPhone": this.data.phone,

      "provinceName": addressArry[0],
      "cityName": addressArry[1],
      "regionName": addressArry[2],

      "defaultFlag": this.data.checked?1:0,// 1默认 0不选中
      "detailAddress": this.data.detailAddress,
     
      "addressId": this.data.descObj.addressId,
      "userId": this.data.descObj.userId,
     })
     if(result.resultCode==200){
      wx.navigateTo({
        url: '/addressPackage/pages/addresslist/addresslist',
      })
    }
  },
})